package com.yuntian.controller;


import com.yuntian.pojo.Result;
import com.yuntian.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

/*    //本地存储
    @PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws Exception {
        log.info("文件上传");
        //获取原始文件名
        String originaFilename = image.getOriginalFilename();
        //构造唯一文件名  --uuid（统一唯一识别码） eg:91ca253b-d6bc-442f-a519-74acd85814fb
        int index = originaFilename.lastIndexOf(".");
        String extname =  originaFilename.substring(index);
        String newFileName = UUID.randomUUID().toString()+extname;
        log.info("新的文件名:{}",newFileName);
    //将文件转存到某个磁盘目录下
        image.transferTo(new File("C:\\Users\\28618\\Desktop\\新建文件夹\\垃圾桶"+newFileName));
        return Result.success();
    }*/
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传，文件名:{}",image.getOriginalFilename());
        //调用阿里云oss工具类
        String url = aliOSSUtils.upload(image);
        log.info("文件上传完成，文件访问的url:{}",url);
        return Result.success(url);
    }
}
